package com.manyTomany.both_way.inverse.service;

import com.manyTomany.both_way.inverse.model.Category;
import com.manyTomany.both_way.inverse.model.Item;
import com.utils.HibernateUtil;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import java.util.List;

public class BusinessService {

    private HibernateUtil hibernateUtil = new HibernateUtil();
    private SessionFactory sessionFactory = hibernateUtil.getSessionFactory();

    private void saveCategory(Category category) {
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            session.save(category);
            tx.commit();
            System.out.println("");
        } catch (RuntimeException e) {
            if (tx != null) {
                tx.rollback();
            }
            throw e;
        } finally {
            session.close();
        }
    }

    private Category loadCategory(Long id) {
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            Category category = (Category) session.get(Category.class, id);

            List items = category.getItems();
            for (Object item1 : items) {
                Item item = (Item) item1;
                Hibernate.initialize(item.getCategories());
            }
            tx.commit();
            return category;
        } catch (RuntimeException e) {
            if (tx != null) {
                tx.rollback();
            }
            throw e;
        } finally {
            session.close();
        }
    }

    private void printCategory(Category category) {
        System.out.println("The category(" + category.getName() + ") is associated with " + category.getItems().size() + " items");
        List items = category.getItems();
        for (Object item1 : items) {
            Item item = (Item) item1;
            System.out.println(category.getName() + " " + item.getName() + "       " + item.getBasePrice());
            System.out.println("The item(" + item.getName() + ") is associated with " + item.getCategories().size() + " categories");
        }
    }

    private void test() {

        Item item1 = new Item("NEC500", 1000);
        Item item2 = new Item("BELL4560", 1800);

        Category category1 = new Category();
        category1.setName("CellPhone");
        category1.getItems().add(item1);
        category1.getItems().add(item1);
        category1.getItems().add(item2);
        item1.getCategories().add(category1);
        item1.getCategories().add(category1);
        item2.getCategories().add(category1);

        Category category2 = new Category();
        category2.setName("NECSeries");
        category2.getItems().add(item1);
        item1.getCategories().add(category2);

        saveCategory(category1);
        saveCategory(category2);

        category1 = loadCategory(category1.getId());
        printCategory(category1);

        sessionFactory.close();
    }

    public static void main(String args[]) {
        new BusinessService().test();
    }
}
